package guitest;

public class ProgramWindow extends javax.swing.JFrame {
	private static final long serialVersionUID = 1L;
	
	core.ProcessorClass processor = null;

	public ProgramWindow(core.ProcessorClass p){
		this.processor = p;
		this.init();
	}
	protected void prepare_exit(){
		//Operations to prepare exit of the program
		System.exit(0);
	}
	private void init(){
		this.setSize(new java.awt.Dimension(120,300));
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				System.out.println("finestra tancada");
				prepare_exit();
			}
		});
		this.setTitle("SEP16 Simulator");
		this.setContentPane(this.getProgramPanel());
		this.setVisible(true);
	}

	//RegisterPanel
	javax.swing.JList programList = null;
	javax.swing.JPanel programPanel = null;

	public void setProgramListData(java.util.Vector<java.lang.Integer> v){
		java.util.Iterator<java.lang.Integer> i = v.iterator();
		java.util.Vector<java.lang.String> res = new java.util.Vector<java.lang.String>();
		core.InstructionClass auxInstruction = null;
		short memValue;
		short posValue;
		java.lang.Integer aux;
		while(i.hasNext()){
			aux = i.next();
			memValue = (short)(aux.intValue() & 0x0000FFFF);
			posValue = (short)((aux.intValue() & 0xFFFF0000)>>16);
			auxInstruction = new core.InstructionClass(memValue);
			if(auxInstruction.inst.getFormat() == 4){
				if(i.hasNext()){
					aux = i.next();
					memValue = (short)(aux.intValue() & 0x0000FFFF);
					auxInstruction.setByteCode2(memValue);
					res.add(java.lang.String.format("0x%04X %s", posValue, auxInstruction.getAssembly()));
					res.add(java.lang.String.format("0x%04X ", posValue+1));
				}
			}else{
				res.add(java.lang.String.format("0x%04X %s", posValue, auxInstruction.getAssembly()));
			}
		}
		this.programList.setListData(res);
	}
	private void reloadProgramListValues(short mempos, short n){
		this.setProgramListData(this.processor.mem.getMemoryVector(mempos, n));
		this.programField.setText(java.lang.String.format("0x%04X", mempos));
	}
	private void reloadProgramListValues(){
		this.reloadProgramListValues((short)0x0000, (short)60);
	}

	private javax.swing.JComponent getProgramList(){
		this.programList = new javax.swing.JList();
		this.programList.setCellRenderer(new guitest.ProgramWindow.ProgramRender());
		return(this.programList);
	}

	javax.swing.JTextField programField = null;
	private short lastProgram = 0;
	private void changeProgramListValues(){
		short newProgram = 0;
		try{
			newProgram = java.lang.Integer.decode(this.programField.getText()).shortValue();
			this.lastProgram = newProgram;
			this.reloadProgramListValues(this.lastProgram, (short)60);
		}catch(java.lang.NumberFormatException e){
		}

	}
	private javax.swing.JPanel getProgramDirectionPanel(){
		javax.swing.JPanel auxPDP = new javax.swing.JPanel();

		this.programField = new javax.swing.JTextField("0x0000", 5);
		this.programField.addActionListener(new java.awt.event.ActionListener(){
			public void actionPerformed(java.awt.event.ActionEvent e){
				changeProgramListValues();
			}
		});
		auxPDP.add(this.programField);
		
		javax.swing.JButton auxJB = new javax.swing.JButton("PC");
		auxJB.addActionListener(new java.awt.event.ActionListener(){
			public void actionPerformed(java.awt.event.ActionEvent e){
				reloadProgramListValues(processor.cu.getPC(), (short)60);
			}
		});
		auxPDP.add(auxJB);
		return(auxPDP);
	}
	private javax.swing.JPanel getProgramPanel(){
		this.programPanel = new javax.swing.JPanel(new java.awt.BorderLayout());
		this.programPanel.add(this.getProgramDirectionPanel(), java.awt.BorderLayout.NORTH);
		this.programPanel.add(this.getProgramList(), java.awt.BorderLayout.CENTER);
		return(this.programPanel);
	}

	class ProgramRender extends javax.swing.JPanel implements javax.swing.ListCellRenderer{
		static final long serialVersionUID = 0;
		javax.swing.JTextField t = new javax.swing.JTextField();
		javax.swing.JTextField pos = new javax.swing.JTextField(6);

		public java.awt.Component getListCellRendererComponent(javax.swing.JList list, java.lang.Object value, int index, boolean selected, boolean focused) {
			//Extremely dirty trick for java: two shorts are encoded into a Integer
			java.lang.String aux = (java.lang.String) value;

			//this.setLayout(new java.awt.GridLayout(0,2));
			this.setLayout(new java.awt.BorderLayout());
			t.setText(aux.substring(7));
			t.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
			t.setBorder(null);
			pos.setBorder(null);
			pos.setText(aux.substring(0, 6));
			pos.setBackground(java.awt.Color.lightGray);
			pos.setSize(pos.getWidth() + 40, pos.getHeight());
			this.add(pos, java.awt.BorderLayout.WEST);
			this.add(t, java.awt.BorderLayout.CENTER);
			return(this);
		}
	}

	public static void main(String args[]){
		core.ProcessorClass p = new core.ProcessorClass();
		guitest.ProgramWindow pw =	new ProgramWindow(p);
		p.loadFile(new java.io.File("prog1.s16"));
		p.run();
		pw.reloadProgramListValues();
	}
}
